//
//  ViewController.m
//  WriteFileDemo
//
//  Created by UniHan_iMac on 17/3/31.
//  Copyright © 2017年 UniHan_iMac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取文件路径
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"fileName.plist"];
    
    // 存数据
    NSError *error = nil;
    BOOL isSuccess = [@"字符串" writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if(isSuccess){
        NSLog(@"写入成功");
    }
    else{
        NSLog(@"写入失败：%@",error);
    }
    
    NSArray *array = @[[NSNumber numberWithInt:18],@"20"];
    [array writeToFile:filePath atomically:YES];
    
    NSDictionary *dictionary = @{@"key":@"value"};
    [dictionary writeToFile:filePath atomically:YES];

    NSData *data = [@"字符串转二进制数据" dataUsingEncoding:NSUTF8StringEncoding];
    [data writeToFile:filePath atomically:YES];
    
    // 读数据
    NSString *string = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",string);

    NSLog(@"%@",[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]);
    
    NSLog(@"%@",[NSArray arrayWithContentsOfFile:filePath]);
    
    NSLog(@"%@",[NSDictionary dictionaryWithContentsOfFile:filePath]);

    NSLog(@"%@",[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:filePath] encoding:NSUTF8StringEncoding]);
}






@end
